package com.ciwong.epaper.modules.me.ui;

import android.content.DialogInterface;
import android.view.KeyEvent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.mobilelib.widget.CWDialog;

/**
 * 在线客服浏览器
 *
 * @author sugy
 * @version ciwong v.1.0 2015/9/8 10:59
 * @since ciwong v.1.0
 */
public class OnlineServiceActivity extends BaseHtmlActicity {

    @Override
    public void onLoadOver() {

    }

    @Override
    public void loadData() {
        super.loadData();
        String title = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
        String url = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_URL);
        setTitleText(title);
        setStartURL(url);
        loadUrl(url);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            confirm();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 是否退出
     */
    private void confirm() {
        CWDialog dialog = new CWDialog(this);
        dialog.setMessage(R.string.confirm_end_online_service);
        dialog.setCancelable(true);
        dialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (webView != null) {
                            webView.loadUrl("javascript:Live800AppConnector.closeChat()");
                        }
                        dialog.dismiss();
                        finish();
                    }
                }).setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }
}
